<script setup>
import PageContainer from '@/components/PageContainer.vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { updatePwdService } from '@/api/user'
import { useRouter } from 'vue-router'
const router = useRouter()
import { useUserStore } from '@/stores'
const userStore = useUserStore()
const pwdFormRef = ref(null)
const pwdForm = ref({
  old_pwd: '',
  new_pwd: '',
  re_pwd: ''
})
// 验证新密码
const validateNewPwd = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入新密码'))
  } else if (value === pwdForm.value.old_pwd) {
    callback(new Error('新密码不能与原密码相同'))
  } else {
    callback()
  }
}
// 验证新密码
const validateRePwd = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入新密码'))
  } else if (value !== pwdForm.value.new_pwd) {
    callback(new Error('两次输入的新密码不一致'))
  } else {
    callback()
  }
}
// 表单验证规则
const rules = {
  old_pwd: [
    { required: true, message: '请输入原密码', trigger: 'blur' },
    { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
  ],
  new_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' },
    { validator: validateNewPwd, trigger: 'blur' }
  ],
  re_pwd: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' },
    { validator: validateRePwd, trigger: 'blur' }
  ]
}
// 提交表单
const submitForm = async () => {
  try {
    await pwdFormRef.value.validate()
    await updatePwdService(pwdForm.value)
    ElMessage.success('密码更新成功')
    userStore.logout()
    router.push('/login')
  } catch (error) {
    console.error(error)
    ElMessage.error('密码更新失败')
  }
}
// 重置表单
const resetForm = () => {
  pwdFormRef.value.resetFields()
}
</script>
<template>
  <page-container title="修改密码">
    <el-form
      ref="pwdFormRef"
      :model="pwdForm"
      :rules="rules"
      label-width="80px"
      class="password-update-form"
    >
      <el-form-item label="原密码" prop="old_pwd">
        <el-input
          v-model="pwdForm.old_pwd"
          show-password
          placeholder="请输入原密码"
        />
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input
          v-model="pwdForm.new_pwd"
          show-password
          placeholder="请输入新密码"
        />
      </el-form-item>
      <el-form-item label="确认密码" prop="re_pwd">
        <el-input
          v-model="pwdForm.re_pwd"
          show-password
          placeholder="请再次输入新密码"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">修改密码</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form></page-container
  >
</template>
<style scoped>
.password-update-form {
  max-width: 400px;
  margin: 0 auto;
}
</style>
